//
//  FCSiftCollectionView.m
//  siftSearch_Example
//
//  Created by WJC on 2019/5/7.
//  Copyright © 2019 64912907. All rights reserved.
//

#import "FCSiftCollectionView.h"
#import "Masonry.h"
#import "WJCDefine.h"
#import "UIView+returnBlock.h"
@interface FCSiftCollectionView()<UICollectionViewDelegate,UICollectionViewDataSource>
@end
@implementation FCSiftCollectionView
-(void)setSiftArr:(NSMutableArray<WJCSiftModel *> *)siftArr{
    _siftArr=siftArr;
    [self.collectionView reloadData];
}
-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
            //    //设置layout水平滑动
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];  //设置head大小
        flowLayout.itemSize=CGSizeMake(WJCScreenW * 0.24, 32);
        flowLayout.minimumInteritemSpacing=1;
        flowLayout.sectionInset=UIEdgeInsetsMake(2, 0, 2, 0);
        
        self.collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 25,WJCScreenW , 90) collectionViewLayout:flowLayout];
        self.collectionView.backgroundColor=[UIColor clearColor];
        [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"commonItem"];
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.top.and.right.and.bottom.mas_equalTo(0);
        }];
        self.collectionView.scrollEnabled=NO;
        self.collectionView.delegate=self;
        self.collectionView.dataSource=self;
    }
    return self;
}
#pragma mark--collectionDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
     [collectionView.collectionViewLayout invalidateLayout];
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.siftArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"commonItem" forIndexPath:indexPath];
    if (!cell) {
        cell=[[UICollectionViewCell alloc] init];
    }
    [cell.contentView removeChildViews];
    createCommonLabel(titleLabel, font12, HEXC(0x646262))
    titleLabel.text=self.siftArr[indexPath.row].Name;
    titleLabel.frame=CGRectMake(0,self.collectionHeight>0? self.collectionHeight*0.5-10 : 6, cell.contentView.width, 20);
    titleLabel.numberOfLines=0;
    titleLabel.textAlignment=NSTextAlignmentCenter;
    [cell.contentView addSubview:titleLabel];
    if (self.buleBack) {
        cell.contentView.backgroundColor=[UIColor whiteColor];
        cell.contentView.layer.borderWidth=0.5f;
        cell.contentView.layer.borderColor=HEXC(0xD8D8D8).CGColor;
        
    }else{
        cell.contentView.backgroundColor=self.noChoiceBackolor? self.noChoiceBackolor :  HEXC(0xF5F5F5);
        cell.contentView.layer.borderWidth=0.5f;
        cell.contentView.layer.borderColor=self.noChoiceTxtColor? self.noChoiceTxtColor.CGColor :  HEXC(0xF5F5F5).CGColor;
    }
    
        if (self.choiceArr.count<1) {
            if (self.hasAll) {
                if (indexPath.row==0) {
                    if (self.buleBack) {
                        cell.contentView.backgroundColor=HEXC(0x6291F6);
                        titleLabel.textColor=[UIColor whiteColor];
                    }else{
                        cell.contentView.backgroundColor=self.choiceBackolor? self.choiceBackolor : WJCColor(234, 243, 255);
                        titleLabel.textColor=self.choiceTxtColor? self.choiceTxtColor : HEXC(0x238EFA);
                        
                    }
                    cell.contentView.layer.borderColor=self.choiceBackolor? self.choiceBackolor.CGColor : [UIColor whiteColor].CGColor;
                }
        }
        }
   
    for (WJCSiftModel *sift in _choiceArr) {
        if ([sift.Id isEqual:self.siftArr[indexPath.row].Id]) {
            if (self.buleBack) {
                cell.contentView.backgroundColor=HEXC(0x6291F6);
                titleLabel.textColor=[UIColor whiteColor];
            }else{
                cell.contentView.backgroundColor=self.choiceBackolor? self.choiceBackolor :WJCColor(234, 243, 255);
                titleLabel.textColor=self.choiceTxtColor? self.choiceTxtColor : HEXC(0x238EFA);
                
            }
            cell.contentView.layer.borderColor=self.choiceTxtColor? self.choiceTxtColor.CGColor : [UIColor whiteColor].CGColor;
        }
    }
    
    cell.contentView.layer.cornerRadius=4;
    cell.contentView.layer.masksToBounds=YES;
    for (WJCSiftModel *sift in self.canNotChoiceArr) {
        if ([sift.Id isEqual:self.siftArr[indexPath.row].Id]) {
            cell.contentView.backgroundColor=HEXC(0xE3E3E3);
            titleLabel.textColor=HEXC(0x646262);
        }
    }
    return cell;
    
    
}
    //  返回头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
        //如果是头视图
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"imageHeaderView" forIndexPath:indexPath];
        [header removeChildViews];
        
        return header;
    }
    
    return nil;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if (!_choiceArr) {
        _choiceArr=[NSMutableArray new];
    }
    for (WJCSiftModel *sift in self.canNotChoiceArr) {
        if ([sift.Id isEqual:self.siftArr[indexPath.row].Id]) {
            if (self.idBlock) {
                self.idBlock(self.siftArr[indexPath.row].Id);
            }
            return;
        }
    }
    if (self.maxChoice==1) {
        [_choiceArr removeAllObjects];
        [_choiceArr addObject:self.siftArr[indexPath.row]];
       
        if (self.voidBlock) {
            self.voidBlock();
        }
         [collectionView reloadData];
    }else
        {
        if (self.hasAll&&indexPath.row==0) {
            self.choiceArr=[NSMutableArray new];
            if (self.voidBlock) {
                self.voidBlock();
            }
            [collectionView reloadData];
            return;
        }
    for (WJCSiftModel *sift in _choiceArr) {
        if ([sift.Id isEqual:self.siftArr[indexPath.row].Id]) {
            [_choiceArr removeObject:self.siftArr[indexPath.row]];
            if (self.voidBlock) {
                self.voidBlock();
            }
            [collectionView reloadData];
            return;
        }
    }
        if (self.maxChoice>0&&self.maxChoice<_choiceArr.count) {
            [SVProgressHUD showErrorWithStatus:@"无法选择更多类别"];
            return;
        }
    [_choiceArr addObject:self.siftArr[indexPath.row]];
        if (self.voidBlock) {
            self.voidBlock();
        }
    [collectionView reloadData];
    }
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
//设置纵向的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 12.0f;
}
//设置单元格间的横向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0.01f;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(self.collectionWidth? self.collectionWidth : WJCScreenW*0.21 , self.collectionHeight?self.collectionHeight:32);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
